<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/table.php";

class Konto extends table {
    
    public static $TABLENAME = "konto";
    public static $CLASSNAME = "Konto";
    
    private $id = "";
    private $name = "";
    private $blz = "";
    private $nr = "";
    private $iban = "";
    private $bic = "";
    
    /**
     * Constructor
     * 
     * @param int $id optional the id of the element to open from database.
     *                if not set an empty element will be created.
     */
    public function __construct($id="", $blz="", $kontonr="") {
        global $VEREIN;
        if($id!='') {
            $temp = Database::getDefault()->openId(Konto::$TABLENAME,$id);
            if($temp!=null) {
                foreach($temp as $key => $value) {
                    $this->$key = $value;
                }
            }
        } else if($blz!="" && $kontonr!="") {
            $query = "SELECT * FROM " . $VEREIN['prefix'] . Konto::$TABLENAME . " WHERE blz='".$blz."' AND nr='" . $kontonr . "'";
            $temp = Database::getDefault()->execute($query, Konto::$CLASSNAME);
            if($temp!= null && sizeof($temp)) {
                foreach($temp[0] as $key => $value) {
                    $this->$key = $value;
                }
            }
        }
    }
    
    public function delete() {
        Database::getDefault()->delete(Konto::$TABLENAME,$this->id);
    }
    
    public function save() {
        $values = array();
        foreach ($this as $key => $value) {
            if($key == "id") {
                continue;
            }
            $values[$key] = $value;
        }
        if($this->id=="") {
            $this->id = Database::getDefault()->insert(Konto::$TABLENAME, $values);
        } else {
            Database::getDefault()->update(Konto::$TABLENAME, $values, $this->id);
        }
    }
    
    /**
     * Get all groups
     */
    static public function getAll() {
        global $VEREIN;
        $query = "SELECT * FROM " . $VEREIN['prefix'] . Konto::$TABLENAME . " ORDER BY name";
        return Database::getDefault()->execute($query,Konto::$CLASSNAME);
    }

    public function getId() {
        return $this->id;
    }
    
    public function setName($value) {
        $this->name = $value;
    }
    
    public function getName() {
        return $this->name;
    }
    
    public function setNumber($value) {
        $this->nr = $value;
    }
    
    public function getNumber() {
        return $this->nr;
    }
    
    public function setBlz($value) {
        $this->blz = $value;
    }
    
    public function getBlz() {
        return $this->blz;
    }
    
    public function setIban($value) {
        $this->iban = $value;
    }
    
    public function getIban() {
        return $this->iban;
    }
    
    public function setBic($value) {
        $this->bic = $value;
    }
    
    public function getBic() {
        return $this->bic;
    }
    
    /**
     * create table for addresses or update if something to do
     */
    public static function initDatabase() {
        global $VEREIN;
        echo "<b>create / update konto</b><br />";
        $query = "CREATE TABLE IF NOT EXISTS " . $VEREIN['prefix'] . "konto (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            die(Database::getDefault()->getConnection()->error);
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "konto ADD COLUMN name VARCHAR(100) NOT NULL";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "konto ADD COLUMN blz VARCHAR(8) NOT NULL";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "konto ADD COLUMN nr VARCHAR(10) NOT NULL";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "konto ADD COLUMN iban VARCHAR(22) DEFAULT NULL";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "konto ADD COLUMN bic VARCHAR(11) DEFAULT NULL";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        echo "<br />";
    }

}
?>